package com.crm.service.saling.impl;

import com.crm.dao.saling.RefundDetailsDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.DispatcherDetail;
import com.crm.pojo.RefundDetail;
import com.crm.service.saling.RefundDetailService;
import com.crm.vo.RefundDetailProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dr5hx on 2018/8/24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RefundDetailServiceImpl implements RefundDetailService {
    @Autowired
    private RefundDetailsDao refundDetailsDao;

    /**
     * 保存一个退款详单
     *
     * @param list
     * @return
     */
    @Override
    public List<RefundDetail> saveRefundDetailList(List<RefundDetail> list) throws ServiceException {
        try {
            Iterable iterable = refundDetailsDao.save(list);
            List<RefundDetail> refundDetailList = new ArrayList<>();
            iterable.forEach(e -> refundDetailList.add((RefundDetail) e));
            return refundDetailList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }
    @Override
    public void toCheck(Long rid){
        List<RefundDetail> rlist = refundDetailsDao.queryAllByrid(rid);
        for (RefundDetail refundDetail:rlist) {
            refundDetail.setFkRefundId(new BigDecimal(1));//审核通过
            refundDetailsDao.save(refundDetail);
        }
    }

    @Override
    public List<RefundDetailProductVO> getRefundDetailProductVO(int page, int rows) throws ServiceException {
        try {
            return refundDetailsDao.getRefundDetailProductVO(new PageRequest(page - 1, rows));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public Long getRefundDetailCount() {
        return refundDetailsDao.getRefundDetailCount();
    }
}
